/* 
 * 创建日期 2011-7-29
 *
 * 成都天和软件公司
 * 电话：028-85425861 
 * 传真：028-85425861-8008 
 * 邮编：610041 
 * 地址：成都市武侯区航空路6号丰德万瑞中心B座1001 
 * 版权所有
 */
package  org.fantasy.common.db.bean;

import java.util.Date;

import  org.fantasy.common.util.StringUtil;

/**
 * ColumnBean
 * 
 * @author 王文成
 * @version 1.0
 * @since 2011-7-29
 */
public class ColumnBean {
	
	/**
	 * 列名
	 */
	private String name;
	
	/**
	 * 转换后的字段名
	 */
	private String field;

	/**
	 * 类型
	 */
	private String type;
	
	/**
	 * 长度
	 */
	private int size;
	
	/**
	 * 是否允许为空
	 */
	private boolean nullable;
	
	/**
	 * 小数点
	 */
	private int scale;
	
	/**
	 * 注释
	 */
	private String comment;

	public ColumnBean(String name, String type, int size, boolean nullable, int scale , String comment) {
		this.name = name;
		this.type = type;
		this.size = size;
		this.nullable = nullable;
		this.scale = scale;
		this.comment = comment;
		this.field = StringUtil.getFieldString(name);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public boolean isNullable() {
		return nullable;
	}

	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}

	public int getScale() {
		return scale;
	}

	public void setScale(int scale) {
		this.scale = scale;
	}

	public String getField() {
		return field;
	}
	
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
	
	@SuppressWarnings("all")
	private String getDataType(){
		try {
			Class cls = Class.forName(type);
			if( Number.class.isAssignableFrom(cls) ){
				return "number";
			}
			if( Date.class.isAssignableFrom(cls) ){
				return "date";
			}
		} catch (ClassNotFoundException e) {
		}
		return "string";
	}
	
	public String toColumnXml(){
		StringBuilder xml = new StringBuilder(512);
		xml.append("<column> \n");
		xml.append("	<attribute param=\"fieldName\">" + field + "</attribute> \n");
		xml.append("	<attribute param=\"fieldLabel\">" + comment + "</attribute> \n");
		String visible = String.valueOf(!name.toLowerCase().contains("id"));
		xml.append("	<attribute param=\"visible\">" + visible + "</attribute> \n");
		xml.append("	<attribute param=\"dataType\">" + getDataType() + "</attribute> \n");
		xml.append("</column> \n");
		return xml.toString();
	}

	@Override
	public String toString() {
		return "\n" + toColumnXml() + "\n";
	}
}
